package cn.com.miaoto.business.service;

import cn.com.miaoto.base.common.BaseService;
import cn.com.miaoto.base.common.ExcelBean;
import cn.com.miaoto.base.utils.ExcelUtils;
import cn.com.miaoto.business.model.Company;
import cn.com.miaoto.business.model.Custom;
import cn.com.miaoto.business.mysql.CompanyMapper;
import cn.com.miaoto.business.mysql.CustomMapper;
import cn.com.miaoto.business.vo.CustomInfo;
import cn.com.miaoto.model.LoginUser;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.*;

/**
 * Created by guo.chen on 2018/9/15.
 */
@Service
public class CompanyService extends BaseService<Company>{

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private CustomMapper customMapper;

    /**
     * 删除客户单位同时删除关联的客户信息
     * @param ids
     * @return
     */
    public int deleteAllByIds(String ids){
        deleteByIds(ids);
        return companyMapper.deleteRelation();
    }

    public XSSFWorkbook exportExcel() throws Exception{
        List<CustomInfo> customInfoList= customMapper.selectCustomInfo(new HashMap());
        List<ExcelBean> ems=new ArrayList<>();
        Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
        XSSFWorkbook book=null;
        ems.add(new ExcelBean("客户单位","companyName",0));
        ems.add(new ExcelBean("部门名称","department",0));
        ems.add(new ExcelBean("单位地址","address",0));
        ems.add(new ExcelBean("客户名称","customName",0));
        ems.add(new ExcelBean("联系电话","phone",0));
        ems.add(new ExcelBean("邮箱","email",0));
        ems.add(new ExcelBean("职位","job",0));
        map.put(0, ems);
        book= ExcelUtils.createExcelFile(CustomInfo.class, customInfoList, map, "客户信息");
        return book;
    }

    public void uploadExcel(InputStream in, MultipartFile file, LoginUser loginUser) throws Exception {
        List<List<Object>> listob = ExcelUtils.getBankListByExcel(in, file.getOriginalFilename());
        for (int i = 0; i < listob.size(); i++) {
            List<Object> ob = listob.get(i);
            Condition condition = new Condition(Company.class);
            condition.createCriteria()
                    .andEqualTo("companyName", String.valueOf(ob.get(0)))
                    .andEqualTo("department", String.valueOf(ob.get(1)));
            List<Company> companyList = companyMapper.selectByCondition(condition);
            Company company = new Company();
            if(companyList!=null && companyList.size()>0){
                company = companyList.get(0);
            }else{
                company.setCompanyName(String.valueOf(ob.get(0)));
                company.setDepartment(String.valueOf(ob.get(1)));
                company.setAddress(String.valueOf(ob.get(2)));
                company.setCreator(loginUser.getUserId());
                company.setCreateTime(LocalDateTime.now());
                companyMapper.insertUseGeneratedKeys(company);
            }

            Custom custom = new Custom();
            custom.setCompanyId(company.getId());
            custom.setCustomName(String.valueOf(ob.get(3)));
            custom.setPhone(String.valueOf(ob.get(4)));
            custom.setEmail(String.valueOf(ob.get(5)));
            custom.setJob(String.valueOf(ob.get(6)));
            custom.setCreator(loginUser.getUserId());
            custom.setCreateTime(LocalDateTime.now());
            customMapper.insert(custom);
        }
    }
}
